Passing on name–value arguments

last modified

2023–11–7

It is possible to pass on name–value arguments e.g. from a wrapper function to an implementation function, including preserving default values.

impl has three name–value parameters with default values:

impl.m
function impl(kwargs)

arguments
    kwargs.a = 1;
    kwargs.b = 2;
    kwargs.c = 3;
end

disp(kwargs)

wrapper makes one of them required, but preserves the other two including their default values.

wrapper.m
function wrapper(a, kwargs)

arguments
    a         double;
    kwargs.b  double;
    kwargs.c  double;
end

disp(a)
disp(kwargs)

kwargs_cell = namedargs2cell(kwargs);
impl(kwargs_cell{:}, a=a)